package com.sdleyou.treepro.business.api.user;

import com.sdleyou.treepro.common.model.utils.ResultVOUtil;
import com.sdleyou.treepro.common.model.vo.ResultVO;
import com.sdleyou.treepro.common.service.ContactManager;
import com.sdleyou.treepro.common.service.UserManager;
import com.sdleyou.treepro.common.utils.IpUtil;
import com.sdleyou.treepro.system.version.ApiVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserManager userManager;
    @Autowired
    private ContactManager contactManager;

    /**
     * 用户登陆
     *
     * @param code
     * @param inviteUserId
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/user/login")
    public ResultVO userLogin(
            @RequestParam("code") String code,
            String avatarUrl,
            String nickName,
            String gender,
            String city,
            String province,
            String inviteUserId,
            HttpServletRequest request) {
        //获取IP地址
        String ipAddress = IpUtil.getIpAddr(request);
        return ResultVOUtil.success(userManager.login(code, avatarUrl, nickName, gender, city, province, ipAddress, inviteUserId));
    }

    @ApiVersion(1)
    @PostMapping("/{version}/user/setInfo")
    public ResultVO setUserInfo(
            @RequestParam("userId") String userId,
            @RequestParam("avatarUrl") String avatarUrl,
            @RequestParam("nickName") String nickName,
            @RequestParam("gender") String gender,
            @RequestParam("city") String city,
            @RequestParam("province") String province) {
        return ResultVOUtil.success(userManager.setUserInfo(userId, avatarUrl, nickName, gender, city, province));
    }

    /**
     * 获取用户信息
     */
    @ApiVersion(1)
    @PostMapping("/{version}/user/info")
    public ResultVO userInfo(@RequestParam("user_id") String userId) {
        return ResultVOUtil.success(userManager.userInfo(userId));
    }

    /**
     * 已养成的树木兑换能量接口
     */
    @ApiVersion(1)
    @PostMapping("/{version}/user/exchange")
    public ResultVO exchangeValue(@RequestParam("user_id") String userId, @RequestParam("tree_id") String treeId) {
        return ResultVOUtil.success(userManager.exchangeValue(userId, treeId));
    }

    @ApiVersion(1)
    @PostMapping("/{version}/user/role")
    public ResultVO chooseRole(@RequestParam(name = "role", defaultValue = "PERSONAL") String role,
                               @RequestParam("user_id") @NotEmpty(message = "user_id不能为空") String userId) {
        userManager.chooseRole(userId, role);
        return ResultVOUtil.success();
    }

    /**
     * 获取微信小程序的openid
     *
     * @param code
     * @param encryptedData
     * @param iv
     * @return
     * @throws Exception
     */
    @ResponseBody
    @ApiVersion(1)
    @PostMapping("/{version}/user/getRun")
    public ResultVO getOpenid(@RequestParam("code") String code,
                              @RequestParam("encryptedData") String encryptedData,
                              @RequestParam("iv") String iv) throws Exception {
        return ResultVOUtil.success(userManager.getWXStep(code, encryptedData, iv));
    }

    /**
     * 设置用户经纬度
     *
     * @param longitude
     * @param latitude
     * @param userId
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/user/location")
    public ResultVO userLocation(@RequestParam("longitude") @NotEmpty(message = "经度不能为空") String longitude,
                                 @RequestParam("latitude") @NotEmpty(message = "纬度不能为空") String latitude,
                                 @RequestParam("user_id") @NotEmpty(message = "用户id不能为空") String userId) {
        userManager.setLocation(userId, longitude, latitude);
        return ResultVOUtil.success();
    }

    /**
     * 获取用户分享海报
     *
     * @param userId
     * @return
     */
    @ApiVersion(1)
    @GetMapping("/{version}/user/qrcode")
    public ResultVO qrCodePath(@RequestParam("user_id") String userId) {
        return ResultVOUtil.success(userManager.getSharePoster(userId));
    }

    /**
     * 设置用户真实姓名
     *
     * @param userId
     * @param realname
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/user/realname")
    public ResultVO setRealName(String userId, String realname, String idCardNum, String phone) {
        userManager.setRealname(userId, realname, idCardNum, phone);
        return ResultVOUtil.success();
    }

    /**
     * 获取用户志愿者证书
     *
     * @param userId
     * @return
     */
    @ApiVersion(1)
    @GetMapping("/{version}/user/volunteer")
    public ResultVO volunteerCert(@RequestParam("user_id") String userId) {
        return ResultVOUtil.success(userManager.volunteerCert(userId));
    }

    /**
     * 获取日签图
     *
     * @param userId
     * @return
     */
    @ApiVersion(1)
    @GetMapping("/{version}/user/lot")
    public ResultVO getDailyLot(@RequestParam("user_id") String userId) {
        return ResultVOUtil.success(userManager.getTodayLot(userId));
    }

    /**
     * 获取联系我们信息
     *
     * @return
     */
    @ApiVersion(1)
    @PostMapping("/{version}/user/getContactUsInfo")
    public ResultVO getContactUsInfo() {
        return ResultVOUtil.success(contactManager.getContact());
    }

    @ApiVersion(1)
    @GetMapping("/{version}/user/update/grow")
    public ResultVO updateUserGrowValue() {
        userManager.updateUserGrowValue();
        return ResultVOUtil.success();
    }

    /**
     * 偷水或领取水滴
     */
    @ApiVersion(1)
    @PostMapping("/{version}/user/receive/water")
    public ResultVO receiveWater(@RequestParam("user_id") String userId, String friend_id) {
        return ResultVOUtil.success(userManager.receiveWater(userId, friend_id));
    }
}
